import Phaser from "phaser";
import { JoystickController } from '@/util/JoystickController';
import Player from "@/model/characters";

export default class Battle extends Phaser.Scene {
    private player: Player | null = null;
    private joystickController: JoystickController | null = null;
    private pointerDownTime = 0;
    private pressDuration = 500; // 长按时间阈值，单位：毫秒

    constructor() {
        super("battle");
    }
    preload() {
        // 设置背景颜色为蓝色
        this.cameras.main.setBackgroundColor(0xa8888f);
        this.player = new Player(this,this.cameras.main.width / 2, this.cameras.main.height - 55);
        const pika = this.add.sprite(0, 5, "pika");
        pika.setOrigin(0.5);
        // 按比例缩放精灵
        pika.setScale(50 / pika.width, 50 / pika.height);
        this.player.add(pika);
    }
    create() {
        if (this.player) {
            this.joystickController = new JoystickController(this, this.player);
        }

        // 初始化触摸事件
        this.input.on('pointerdown', (pointer: Phaser.Input.Pointer) => {
            this.pointerDownTime = Date.now();
        });

        this.input.on('pointerup', () => {
            if (this.joystickController && this.joystickController.isJoystickVisible) {
                this.joystickController.hideJoystick();
            }
        });

        this.input.on('pointermove', (pointer: Phaser.Input.Pointer) => {
            if (this.joystickController) {
                if (this.joystickController.isJoystickVisible) {
                    this.joystickController.updateJoystick(pointer);
                    this.joystickController.movePlayer(pointer);
                } else if (Date.now() - this.pointerDownTime >= this.pressDuration) {
                    this.joystickController.showJoystick(pointer);
                }
            }
        });
    }
}